#!/usr/bin/env php
<?php
/**************************************************************************
 *
 *   Copyright 2010 American Public Media Group
 *
 *   This file is part of AIR2.
 *
 *   AIR2 is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   AIR2 is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/

require_once 'app/init.php';
require_once APPPATH.'/../tests/Test.php';
require_once APPPATH.'/../tests/models/TestTank.php';
require_once APPPATH.'/../tests/models/TestSource.php';
require_once 'tank/Discriminator.php';

AIR2_DBManager::init();
$conn = AIR2_DBManager::get_master_connection();
$d = new Discriminator();

// setup
$s = new TestSource();
$s->save();
$t = new TestTank();
$t->tank_user_id = 1;
$t->tank_type = Tank::$TYPE_FB;
$t->tank_status = Tank::$STATUS_IMPORTED;
$t->TankSource[0]->src_id = $s->src_id;
$t->save();
$ts = $t->TankSource[0];
$t->clearRelated();


plan(15);

/**********************
 * 1) no alias set
 */
$ts->sa_name = 'AliasName';
$ts->sa_first_name = 'AliasFirst';
$ts->sa_last_name = 'AliasLast';
$ts->sa_post_name = 'AliasPost';
$ts->save();

$lock = $d->run($t->tank_id);
$s->refresh();
$t->refresh();
$ts = $t->TankSource[0];
$t->clearRelated();

is( $lock, true, 'blank - lock' );
is( $t->tank_status, Tank::$STATUS_DISCRIMINATED, 'blank - tank_status' );
is( $ts->tsrc_status, TankSource::$STATUS_DONE, 'blank - tsrc_status' );
is( $d->done_count, 1, 'blank - done count' );
is( count($s->SrcAlias), 1, 'blank - added src_alias' );


/**********************
 * 2) duplicate alias values
 */
$t->tank_status = Tank::$STATUS_IMPORTED;
$t->save();
$ts->tsrc_status = TankSource::$STATUS_NEW;
$ts->tsrc_errors = null;
$ts->save();

$lock = $d->run($t->tank_id);
$s->refresh();
$t->refresh();
$ts = $t->TankSource[0];
$t->clearRelated();

is( $lock, true, 'dup - lock' );
is( $t->tank_status, Tank::$STATUS_DISCRIMINATED, 'dup - tank_status' );
is( $ts->tsrc_status, TankSource::$STATUS_DONE, 'dup - tsrc_status' );
is( $d->done_count, 1, 'dup - done count' );
is( count($s->SrcAlias), 1, 'dup - still just 1 src_alias' );


/**********************
 * 3) changed source values
 */
$t->tank_status = Tank::$STATUS_IMPORTED;
$t->save();
$ts->tsrc_status = TankSource::$STATUS_NEW;
$ts->tsrc_errors = null;
$ts->sa_name = 'AliasName2';
$ts->sa_first_name = 'AliasFirst2';
$ts->sa_last_name = 'AliasLast2';
$ts->sa_post_name = 'AliasPost2';
$ts->save();

$lock = $d->run($t->tank_id);
$s->refresh();
$t->refresh();
$ts = $t->TankSource[0];
$t->clearRelated();

is( $lock, true, 'changed - lock' );
is( $t->tank_status, Tank::$STATUS_DISCRIMINATED, 'changed - tank_status' );
is( $ts->tsrc_status, TankSource::$STATUS_DONE, 'changed - tsrc_status' );
is( $d->done_count, 1, 'changed - done count' );
is( count($s->SrcAlias), 1, 'changed - still just 1 src_alias' );
